import { defineStore } from 'pinia'
import { getCodeInfo } from '@/api/base/base'

export const useCodeStore = defineStore('code', {
    state() {
        return {
            codeInfo: new Map() as Map<string, Array<SystemCode>>
        }
    },
    getters: {},
    actions: {
        async InitCodeInfo(){
            const { data } = await getCodeInfo();
            data.forEach(ele => {
                this.codeInfo.set(ele.dict, ele.codes)
            })
        },
        getDict(dict: string): Array<SystemCode>{
            return this.codeInfo.get(dict)!
        },
        getName(dict: string, value: string): string{
            return this.codeInfo.get(dict)!
                .filter(ele => ele.value === value)
                .map(ele => ele.name)[0]
        }
    }
})
